# 3.实现一个 atoi 函数，使其能将字符串转换成整数
# def my_atoi(s: str) -> int:
#     # 实现代码区域
# print(my_atoi("42"))  # 输出: 42
# print(my_atoi("   -42"))  # 输出: -42
# print(my_atoi("4193 abcd case"))  # 输出: 4193  此题不允许使用int()

def my_atoi(s: str) -> int:
    s_int = 0
    s_sign = 0

    for i in range(0,len(s)):
        if not s_int and s[i] == "-" and (i+1) < len(s):
            if s[i+1].isdigit():
                s_sign = -1
        if s[i].isdigit():
            s_int = s_int * 10 + ord(s[i]) - ord("0")
            if (i+1) < len(s):
                if not s[i+1].isdigit():
                    print(my_atoi(s[i + 1::1]), end="、")
                    break

    if s_sign == -1:
        s_int = 0 - s_int

    for item in s:
        if item.isdigit():
            return s_int


if __name__ == "__main__":
    print(my_atoi("   -42"))
    print(my_atoi("4193 abcd case"))
    print(my_atoi("78-"))
    print(my_atoi("-a78"))
    print(my_atoi("4-1"))


